#!/bin/sh

# Slackware build script for atarisio

# Written by B. Watson (yalhcru@gmail.com)

PRGNAM=atarisio
VERSION=20100113
ARCH=${ARCH:-i486}
BUILD=${BUILD:-1}
TAG=${TAG:-_SBo}

CWD=$(pwd)
TMP=${TMP:-/tmp/SBo}
PKG=$TMP/package-$PRGNAM
OUTPUT=${OUTPUT:-/tmp}

KERNEL=${KERNEL:-$(uname -r)}
KERNELPATH=${KERNELPATH:-/lib/modules/$KERNEL/build}
PKG_VERS=${VERSION}_$(echo $KERNEL | tr - _)

SRCVER=$( echo $VERSION | sed 's/^..//' )

SETUID=${SETUID:-yes}
GROUP=${GROUP:-users}

# No need for LIBDIRSUFFIX here, no /usr/lib dir in package.
if [ "$ARCH" = "i486" ]; then
  SLKCFLAGS="-O2 -march=i486 -mtune=i686"
elif [ "$ARCH" = "i686" ]; then
  SLKCFLAGS="-O2 -march=i686 -mtune=i686"
elif [ "$ARCH" = "x86_64" ]; then
  SLKCFLAGS="-O2 -fPIC"
fi

set -e

rm -rf $PKG
mkdir -p $TMP $PKG $OUTPUT
cd $TMP
rm -rf $PRGNAM-$SRCVER
tar xvf $CWD/$PRGNAM-$SRCVER.tar.gz
cd $PRGNAM-$SRCVER
chown -R root:root .
chmod -R a-s,u+w,go+r-w .

sed -i "s/^\\(C.*FLAGS *= *\\)-g/\1$SLKCFLAGS/" Makefile

make
mkdir -p $PKG/usr/{include,bin} $PKG/lib/modules/$KERNEL/misc
make install INST_DIR=$PKG/usr MDIR=$PKG/lib/modules/$KERNEL/misc

# Hias forgot to add a couple of useful tools to his "make install" rule:
cp tools/casinfo $PKG/usr/bin

strip $PKG/usr/bin/*
chown -R root:root $PKG/usr/bin

if [ "$SETUID" = "yes" ]; then
	chown root:$GROUP $PKG/usr/bin/{atariserver,atarixfer}
	chmod 4710 $PKG/usr/bin/{atariserver,atarixfer}
else
	chmod 0755 $PKG/usr/bin/{atariserver,atarixfer}
fi


mkdir -p $PKG/lib/udev/rules.d
sed 's/@GROUP@/'$GROUP'/g' \
  < $CWD/999-$PRGNAM.rules.in \
  > $PKG/lib/udev/rules.d/999-$PRGNAM.rules

mkdir -p $PKG/usr/man/man1
cd $CWD/man
  for i in *.1; do
    gzip -9c $i > $PKG/usr/man/man1/$i.gz
  done
cd -

mkdir -p $PKG/usr/doc/$PRGNAM-$PKG_VERS
cp $CWD/README.Slackware README LICENSE Changelog $PKG/usr/doc/$PRGNAM-$PKG_VERS
cat $CWD/$PRGNAM.SlackBuild > $PKG/usr/doc/$PRGNAM-$PKG_VERS/$PRGNAM.SlackBuild

mkdir -p $PKG/install
cat $CWD/doinst.sh > $PKG/install/doinst.sh
cat $CWD/slack-desc > $PKG/install/slack-desc

cd $PKG
/sbin/makepkg -l y -c n $OUTPUT/$PRGNAM-$PKG_VERS-$ARCH-$BUILD$TAG.${PKGTYPE:-tgz}
